function isBalanced(root, depth=0) {
    if (root == null) {
        depth = 0;
        return {
            depth: depth,
            flag: true
        };
    }

    let leftRes = isBalanced(root.left, 0),
        rightRes = isBalanced(root.right, 0);

    if (leftRes.flag && rightRes.flag) {
        let diff = leftRes.depth - rightRes.depth;
        if (diff <= 1 && diff >= -1) {
            depth = Math.max(leftRes.depth, rightRes.depth) + 1;
            return {
                depth: depth,
                flag: true
            }
        }
    }

    return {
        depth: depth,
        flag: false
    }
}